package CustomClassFile.CronJob;
import java.util.Date;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

public class MyQuartzManager {
	private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
	private static String JOB_GROUP_NAME = "SOCIAL_JOBGROUP_ZSWS";
	private static String TRIGGER_GROUP_NAME = "SOCIAL_TRIGGERGROUP_ZSWS";
	private static final Log logger = LogFactory.getLog(MyQuartzManager.class);

	public static void addJob(String paramString1, Class paramClass, String paramString2) {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();
			JobDetail localJobDetail = new JobDetail(paramString1, JOB_GROUP_NAME, paramClass);

			CronTrigger localCronTrigger = new CronTrigger(paramString1, TRIGGER_GROUP_NAME);

			localCronTrigger.setCronExpression(paramString2);
			localScheduler.scheduleJob(localJobDetail, localCronTrigger);
			if (!localScheduler.isShutdown()) {
				localScheduler.start();
			}
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":添加任务失败");
		}
	}
	
	public static void addJob(String paramString1, Class paramClass, Date date,Map<String,Object> map) {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();
			JobDetail localJobDetail = new JobDetail(paramString1, JOB_GROUP_NAME, paramClass);
			localJobDetail.getJobDataMap().putAll(map);
			SimpleTrigger simpleTrigger = new SimpleTrigger(paramString1,TRIGGER_GROUP_NAME);  
			simpleTrigger.setStartTime(date);//开始运行时间  
	        simpleTrigger.setRepeatInterval(2000); //运行间隔单位为毫秒  
	        simpleTrigger.setRepeatCount(0);     //运行次数  
	        Date endTime=DateUtil.dateCalculate(date, 59);//59分钟之后任务结束，自动移除
	        simpleTrigger.setEndTime(endTime);
	        simpleTrigger.setJobGroup(JOB_GROUP_NAME);  
	        simpleTrigger.setJobName(paramString1);
			localScheduler.scheduleJob(localJobDetail, simpleTrigger);
			if (!localScheduler.isShutdown()) {
				localScheduler.start();
			}
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":添加任务失败"+paramString1);
			
			removeJob(paramString1);
			logger.info("重新添加任务"+paramString1);
			addJob2(paramString1, paramClass, date, map);
		}
	}
	
	public static void addJob2(String paramString1, Class paramClass, Date date,Map<String,Object> map) {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();
			JobDetail localJobDetail = new JobDetail(paramString1, JOB_GROUP_NAME, paramClass);
			localJobDetail.getJobDataMap().putAll(map);
			SimpleTrigger simpleTrigger = new SimpleTrigger(paramString1,TRIGGER_GROUP_NAME);  
			simpleTrigger.setStartTime(date);//开始运行时间  
	        simpleTrigger.setRepeatInterval(2000); //运行间隔单位为毫秒  
	        simpleTrigger.setRepeatCount(0);     //运行次数  
	        Date endTime=DateUtil.dateCalculate(date, 59);//59分钟之后任务结束，自动移除
	        simpleTrigger.setEndTime(endTime);
	        simpleTrigger.setJobGroup(JOB_GROUP_NAME);  
	        simpleTrigger.setJobName(paramString1);
			localScheduler.scheduleJob(localJobDetail, simpleTrigger);
			if (!localScheduler.isShutdown()) {
				localScheduler.start();
			}
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":添加任务失败"+paramString1);
		}
	}
	
	//新增参数，多少分钟再次执行一次
	public static void addJob(String paramString1, Class paramClass, Date date,Map<String,Object> map,int minutes) {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();
			JobDetail localJobDetail = new JobDetail(paramString1, JOB_GROUP_NAME, paramClass);
			localJobDetail.getJobDataMap().putAll(map);
			SimpleTrigger simpleTrigger = new SimpleTrigger(paramString1,TRIGGER_GROUP_NAME);  
			simpleTrigger.setStartTime(date);//开始运行时间  
	        simpleTrigger.setRepeatInterval(1000*60*minutes); //运行间隔单位为毫秒 ,分钟转换成毫秒 
	        simpleTrigger.setRepeatCount(1);     //重复1次
	        Date endTime=DateUtil.dateCalculate(date, 59);//59分钟之后任务结束，自动移除
	        simpleTrigger.setEndTime(endTime);
	        simpleTrigger.setJobGroup(JOB_GROUP_NAME);  
	        simpleTrigger.setJobName(paramString1);
			localScheduler.scheduleJob(localJobDetail, simpleTrigger);
			if (!localScheduler.isShutdown()) {
				localScheduler.start();
			}
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":添加任务失败");
		}
	}
	public static void addJob(String paramString1, String paramString2, String paramString3, String paramString4,
			Class paramClass, String paramString5) {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();
			JobDetail localJobDetail = new JobDetail(paramString1, paramString2, paramClass);

			CronTrigger localCronTrigger = new CronTrigger(paramString3, paramString4);

			localCronTrigger.setCronExpression(paramString5);
			localScheduler.scheduleJob(localJobDetail, localCronTrigger);
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":添加任务失败");
		}
	}

	public static void modifyJobTime(String paramString1, String paramString2) {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();
			CronTrigger localCronTrigger = (CronTrigger) localScheduler.getTrigger(paramString1, TRIGGER_GROUP_NAME);
			if (localCronTrigger == null) {
				return;
			}
			String str = localCronTrigger.getCronExpression();
			if (!str.equalsIgnoreCase(paramString2)) {
				JobDetail localJobDetail = localScheduler.getJobDetail(paramString1, JOB_GROUP_NAME);

				Class localClass = localJobDetail.getJobClass();
				removeJob(paramString1);
				addJob(paramString1, localClass, paramString2);
			}
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":修改任务时间失败");
		}
	}

	public static void modifyJobTime(String paramString1, String paramString2, String paramString3) {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();
			CronTrigger localCronTrigger1 = (CronTrigger) localScheduler.getTrigger(paramString1, paramString2);
			if (localCronTrigger1 == null) {
				return;
			}
			String str = localCronTrigger1.getCronExpression();
			if (!str.equalsIgnoreCase(paramString3)) {
				CronTrigger localCronTrigger2 = localCronTrigger1;

				localCronTrigger2.setCronExpression(paramString3);

				localScheduler.resumeTrigger(paramString1, paramString2);
			}
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":修改任务时间失败");
		}
	}

	public static void removeJob(String paramString) {
		logger.info("移除任务:"+paramString);
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();

			localScheduler.pauseTrigger(paramString, TRIGGER_GROUP_NAME);

			localScheduler.unscheduleJob(paramString, TRIGGER_GROUP_NAME);

			localScheduler.deleteJob(paramString, JOB_GROUP_NAME);
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":移除任务失败");
		}
	}

	public static void removeJob(String paramString1, String paramString2, String paramString3, String paramString4) {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();

			localScheduler.pauseTrigger(paramString3, paramString4);

			localScheduler.unscheduleJob(paramString3, paramString4);

			localScheduler.deleteJob(paramString1, paramString2);
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":移除任务失败");
		}
	}

	public static void startJobs() {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();
			localScheduler.start();
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":启动emessage任务调度器失败");
		}
	}

	public static void shutdownJobs() {
		try {
			Scheduler localScheduler = gSchedulerFactory.getScheduler();
			if (!localScheduler.isShutdown()) {
				localScheduler.shutdown();
			}
		} catch (Exception localException) {
			logger.error(localException.getMessage() + ":关闭emessage任务调度器失败");
		}
	}
}
